<?php

namespace Addons\BroadCast\Controller;
use Home\Controller\AddonsController;

class BroadCastController extends AddonsController{
    protected $model;
    function _initialize(){
        $this->model = $this->getModel('broadcast');
        parent::_initialize();
        $action = strtolower ( _ACTION );

        $res ['title'] = '群发设置';
        $res ['url'] = addons_url ( 'BroadCast://BroadCast/config' );
        $res ['class'] = $action == 'config' ? 'current' : '';
        $nav [] = $res;

        $res ['title'] = '消息列表';
        $res ['url'] = addons_url ( 'BroadCast://BroadCast/lists' );
        $res ['class'] = $action == 'lists' ? 'current' : '';
        $nav [] = $res;

        $this->assign ( 'nav', $nav );

        $config = getAddonConfig ( 'BroadCast' );
        $config ['cover_url'] = get_cover_url ( $config ['cover'] );
        $this->config = $config;
        $this->assign ( 'config', $config );
    }
    public function lists(){

        $map ['token'] = get_token ();
        session ( 'common_condition', $map );

        $list_data = $this->_get_model_list ( $this->model );

        //
        //$map ['is_show'] = 1;
        $list = M ( 'broadcast' )->where ( $map )->field ( 'id,title' )->select ();
        $cate [0] = '';
        foreach ( $list as $vo ) {
            $cate [$vo ['id']] = $vo ['title'];
        }

        foreach ( $list_data ['list_data'] as &$vo ) {
            $vo ['cate_id'] = intval ( $vo ['cate_id'] );
            $vo ['cate_id'] = $cate [$vo ['cate_id']];
        }
        $this->assign ( $list_data );
        // dump ( $list_data );

        $templateFile = $this->model ['template_list'] ? $this->model ['template_list'] : '';
        $this->display ( $templateFile );
    }
    public function add(){
        $model = $this->model;
        $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) );

        if (IS_POST) {
            $Model = $this->checkAttr ( $Model, $model ['id'] );
            if ($Model->create () && $id = $Model->add ()) {

                $this->success ( '添加' . $model ['title'] . '成功', U ( 'lists?model=' . $model ['name'] ) );
            } else {
                $this->error ( $Model->getError () );
            }
        } else {
            $fields = get_model_attribute ( $model ['id'] );

            $this->assign ( 'fields', $fields );
            $this->meta_title = $model ['title'];

            $this->display ();
        }
    }

    public function edit(){
        $model = $this->model;
        $id = I ( 'id' );

        if (IS_POST) {
            $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) );
            $Model = $this->checkAttr ( $Model, $model ['id'] );
            if ($Model->create () && $Model->save ()) {
                //$this->_saveKeyword ( $model, $id, 'broadcast' );
            } else {
                $this->error ( $Model->getError () );
            }
        } else {
            $fields = get_model_attribute ( $model ['id'] );


            $data = M ( get_table_name ( $model ['id'] ) )->find ( $id );
            $data || $this->error ( '找不到模型' );

            $token = get_token ();
            if (isset ( $data ['token'] ) && $token != $data ['token'] && defined ( 'ADDON_PUBLIC_PATH' )) {
                $this->error ( '你正在编辑的不是属于这个应用的模型' );
            }

            $this->assign ( 'fields', $fields );
            $this->assign ( 'data', $data );
            $this->meta_title =  $model ['title'];

            $this->display ();
        }
    }

    public function send(){
        //actually send the data TO ALL SUBSCRIBER
        $id = $_POST['target_id'];
        $posturl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.get_access_token();

        $content = M('broadcast')->find($id);
        $msg = json_encode(array(
            "filter"=>array(
                "is_to_all"=>true,
            ),

            "text"=>array(
                "content"=>$content['content'],
            ),
            "msgtype"=>"text"
        ));
        $ch = curl_init($posturl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array('Content-Type:application/json',
                'Content-Length: ' . strlen($msg))
        );
        curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $msg );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $res= curl_exec($ch);
        curl_close($ch);
        $res = json_decode ( $res, true );
        if ($res ['errcode'] == 0) {
            $this->success($res ['errmsg'],U ( 'lists?model=' . $this->model ['name'] ));
            return true;
        } else {
            E('错啦！');
            return false;
        }
    }
}
